package com.tbit.uqbike.client.constant;

import com.tbit.common.constant.SystemConstant;
import com.tbit.common.utils.TraceUtil;
import com.tbit.uqbike.client.exception.BaseException;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * 响应代码
 *
 * @author Leon
 * 2013-10-15 上午10:04:24
 */
public class ResponseCode {
    /**
     * 成功
     */
    public final static Integer SUCC = 1;
    /**
     * 失败
     */
    public final static Integer FAIL = 0;


    /**
     * 结果布尔值
     */
    public final static String RET = "ret";
    /**
     * 描述
     */
    public final static String MSG = "msg";
    /**
     * 错误代码
     */
    public final static String CODE = "code";
    /**
     * 结果内容
     */
    public final static String DATA = "data";


    /**
     * 消息响应
     */
    public static void printException(Map<String, Object> map,
                                      BaseException e, HttpServletRequest request) {
        map.put(ResponseCode.RET, ResponseCode.FAIL);

        String msg = e.getMessage();
        if (msg != null && msg.contains("%")) {
            String[] array = msg.split("%");
            RequestContext requestContext = new RequestContext(request);
            String message = requestContext.getMessage(array[1]);
            map.put(ResponseCode.CODE, array[0]);
            map.put(ResponseCode.MSG, message);
        } else {
            map.put(ResponseCode.CODE, "-100");
            map.put(ResponseCode.MSG, e.getMessage());
        }
        map.put(SystemConstant.TRACE_ID, TraceUtil.getTraceIdByLocal());
    }

}
